package gdut.jmxywz.action;

import gdut.jmxywz.been.MessageBoard;
import gdut.jmxywz.been.MessageBoardFile;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.ServletActionContext;

@SuppressWarnings("serial")
public class MessageFileAction extends BaseAction {

	private MessageBoardFile   messageFile;
	private  String            teacherMessageBoardid;
	private MessageBoard       teacherMessageBoard;
	private String             allFile;     //所有附件审核
	
	 public MessageBoardFile getMessageFile() {
		return messageFile;
	}

	public void setMessageFile(MessageBoardFile messageFile) {
		this.messageFile = messageFile;
	}
	
	 public String getTeacherMessageBoardid() {
		return teacherMessageBoardid;
	}

	public void setTeacherMessageBoardid(String teacherMessageBoardid) {
		this.teacherMessageBoardid = teacherMessageBoardid;
	}

	public MessageBoard getTeacherMessageBoard() {
		return teacherMessageBoard;
	}

	public void setTeacherMessageBoard(MessageBoard teacherMessageBoard) {
		this.teacherMessageBoard = teacherMessageBoard;
	}



	public String getAllFile() {
		return allFile;
	}

	public void setAllFile(String allFile) {
		this.allFile = allFile;
	}

	/***
	  * 
	  * 从服务器删除附件
	  * @return
	  * @throws Exception
	  */
	 
	 public String Delete() throws Exception{
		 
		 //messageReplyFile
		 int messageBoardid = 0;
		
		
		
		 String hql = "from MessageBoardFile mbf where ";
		 
		 boolean b = false;
		 
		 System.out.println("==---="+teacherMessageBoardid);
		 
		 if(!"".equals(teacherMessageBoardid)&&teacherMessageBoardid!=null){
				messageBoardid = Integer.parseInt(teacherMessageBoardid);
				
				hql=hql+"mbf.messageBoard.messageBoardid="+messageBoardid;
				
			}else {
				hql =hql+"1=1 ";
			}
		 if(messageFile!=null){
			 
			 hql = hql+"and  mbf.fileId = "+ messageFile.getFileId();
		 }
		 
		 
		 //删除文件
		 System.out.println("==="+hql);
         
		 List fileList = this.getCommonService().findListByHql(hql, null);
		 
		 Iterator iter = fileList.iterator();
		 
		 while(iter.hasNext()){
			 
			 MessageBoardFile mbf =(MessageBoardFile)iter.next();
			 
			 //删除数据库
			 
			 this.getCommonService().deleteObject(mbf);
			 
			 //删除服务器文件
			 
			 String path =ServletActionContext.getRequest().getRealPath("/");
			 
			 path = path+mbf.getFileUrl();
			 
			 System.out.println("===="+path);
			 
			 b = this.deleteFile(path);
			 
			 if(b){
				 System.out.println("服务器上附件删除成功");
			 }else{
				 System.out.println("服务器上附件删除不成功");
			 }
			 
		 }
		 
		
		 
		 
		 return "MessageBoardFileDelete";
	 }
	 /***
	  * 显示附件修改页面
	  * @return
	  * @throws Exception
	  */
	 public String ModifyShow() throws Exception{
		 
		 System.out.println("-----"+messageFile.getFileId());
		 
		 messageFile =(MessageBoardFile) this.getCommonService().findByIntId(messageFile.getFileId(), "gdut.jmxywz.been.MessageBoardFile");
		 
		 
		 return "TeacherMessageBoardFileModifyShow";
	 }
	 
	 /***
	  * 
	  * 修改附件
	  * @return
	  * @throws Exception
	  */
	 public String Modify() throws Exception{
		 
		 System.out.println("====="+messageFile.getFileContext());
		 
		 MessageBoardFile mbf = (MessageBoardFile)this.getCommonService().findByIntId(messageFile.getFileId(), "gdut.jmxywz.been.MessageBoardFile");
		 
		 mbf.setFileContext(messageFile.getFileContext());
		 
		 mbf.setFileName(messageFile.getFileName());
		 
		 this.getCommonService().updateObject(mbf);
		 
		 return "TeacherMessageBoardFileModify";
	 }
	 
	 
	 /***
	  *附件审核为 院内显示
	  * @return
	  * @throws Exception
	  */
	 public String Inside() throws Exception{
		
		 String hql = "from MessageBoardFile mbf where ";
		 
		 
		 if(teacherMessageBoard!=null&&"1".equals(allFile)){
			
		   hql = hql+" mbf.messageBoard.messageBoardid = "+teacherMessageBoard.getMessageBoardid();
		
		 }else{
			 
			 hql = hql+" 1 = 1 ";
		 }
		 if(messageFile!=null){
			 
			 hql=hql+" and  mbf.fileId = "+messageFile.getFileId();
			 
		 }
		 System.out.println("---hql--"+hql);
		 
		 List fileList = this.getCommonService().findListByHql(hql, null);
		 
		 Iterator iter = fileList.iterator();
		 
		 while(iter.hasNext()){
			 
			 MessageBoardFile mbf = (MessageBoardFile)iter.next();
			 
			 mbf.setAuditing("1");
			 
			 try {
				this.getCommonService().updateObject(mbf);
				
				System.out.println("--附件审核为院内可见---");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
		 }
		

	   return "TeacherMessageBoardFileInside";
	 }
		  
	    /***
	     *   Outside 附件审核为对外公布
	     */
	    public String Outside() throws Exception{
			  
	    	 String hql = "from MessageBoardFile mbf where ";
			 
	    	 if(teacherMessageBoard!=null&&"1".equals(allFile)){
				
			   hql = hql+" mbf.messageBoard.messageBoardid = "+teacherMessageBoard.getMessageBoardid();
			
			 }else{
				 
				 hql = hql+" 1 = 1 ";
			 }
			 if(messageFile!=null){
				 
				 hql=hql+" and  mbf.fileId = "+messageFile.getFileId();
				 
			 }
			 System.out.println("---hql--"+hql);
			 
			 List fileList = this.getCommonService().findListByHql(hql, null);
			 
			 Iterator iter = fileList.iterator();
			 
			 while(iter.hasNext()){
				 
				 MessageBoardFile mbf = (MessageBoardFile)iter.next();
				 
				 mbf.setAuditing("2");
				 
				 try {
					this.getCommonService().updateObject(mbf);
					
					System.out.println("--附件审核为院内可见---");
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 
			 }
			
	    	  
	       return "TeacherMessageBoardFileOutside";
		 }
		
		/***
		 * 
		 *    Notallow 附件审核为不通过
		 */
	    public String Notallow() throws Exception{
			  
	    	 String hql = "from MessageBoardFile mbf where ";
			 
	    	 if(teacherMessageBoard!=null&&"1".equals(allFile)){
				
			   hql = hql+" mbf.messageBoard.messageBoardid = "+teacherMessageBoard.getMessageBoardid();
			
			 }else{
				 
				 hql = hql+" 1 = 1 ";
			 }
			 if(messageFile!=null){
				 
				 hql=hql+" and  mbf.fileId = "+messageFile.getFileId();
				 
			 }
			 System.out.println("---hql--"+hql);
			 
			 List fileList = this.getCommonService().findListByHql(hql, null);
			 
			 Iterator iter = fileList.iterator();
			 
			 while(iter.hasNext()){
				 
				 MessageBoardFile mbf = (MessageBoardFile)iter.next();
				 
				 mbf.setAuditing("3");
				 
				 try {
					this.getCommonService().updateObject(mbf);
					
					System.out.println("--附件审核为院内可见---");
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 
			 }
			 
			

			  return "TeacherMessageBoardFileNotallow";
		  }
		
		/***
		 * 
		 *    Rollback 附件审核为未审核
		 */
	    public String Rollback() throws Exception{
			  
	    	 String hql = "from MessageBoardFile mbf where ";
			 
	    	 if(teacherMessageBoard!=null&&"1".equals(allFile)){
				
			   hql = hql+" mbf.messageBoard.messageBoardid = "+teacherMessageBoard.getMessageBoardid();
			
			 }else{
				 
				 hql = hql+" 1 = 1 ";
			 }
			 if(messageFile!=null){
				 
				 hql=hql+" and  mbf.fileId = "+messageFile.getFileId();
				 
			 }
			 System.out.println("---hql--"+hql);
			 
			 List fileList = this.getCommonService().findListByHql(hql, null);
			 
			 Iterator iter = fileList.iterator();
			 
			 while(iter.hasNext()){
				 
				 MessageBoardFile mbf = (MessageBoardFile)iter.next();
				 
				 mbf.setAuditing("0");
				 
				 try {
					this.getCommonService().updateObject(mbf);
					
					System.out.println("--附件审核为院内可见---");
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 
			 }

	  		  return "TeacherMessageBoardFileRollback";
	  	  }
	    
	 
	   /**
	    *  deleteFile  从服务器上删除附件
	    * @param sPath
	    * @return
	    */
		
     public  boolean deleteFile(String sPath) {   
		    boolean  flag = false;   
			File   file = new File(sPath);   
		    // 路径为文件且不为空则进行删除   
		    if (file.isFile() && file.exists()) {   
		        file.delete();   
		        System.out.println("---成功从服务器删除文件---");
		        flag = true;   
		    }   
		    return flag;   
		}  
	
}
